---
layout: "default"
title: "UnsafePointer"
description: "Swift documentation for 'UnsafePointer': A pointer to an object of type T.  This type provides no automated."
keywords: "UnsafePointer,struct,swift,documentation,advancedBy,distanceTo,encode,getMirror,predecessor,successor,debugDescription,hashValue,memory"
root: "/v1.2"
---

<div class="intro-declaration"><code class="language-swift">struct UnsafePointer&lt;T&gt;</code></div>

<div class="discussion comment">
    <p>A pointer to an object of type <code>T</code>.  This type provides no automated
memory management, and therefore the user must take care to allocate
and free memory appropriately.</p>

<p>The pointer can be in one of the following states:</p>

<ul><li><p>memory is not allocated (for example, pointer is null, or memory has
been deallocated previously);</p></li><li><p>memory is allocated, but value has not been initialized;</p></li><li><p>memory is allocated and value is initialized.</p></li></ul>
</div>

<table class="standard">
<tr>
<th id="inheritance">Inheritance</th>
<td>
<code class="inherits">BidirectionalIndexType, CVarArgType, Comparable, DebugPrintable, Equatable, ForwardIndexType, Hashable, NilLiteralConvertible, RandomAccessIndexType, Reflectable, Strideable, _BidirectionalIndexType, _Comparable, _ForwardIndexType, _Incrementable, _PointerType, _RandomAccessIndexType, _Strideable</code>
<span class="viz"><a href="hierarchy/">View Protocol Hierarchy &rarr;</a></span>
</td>
</tr>



<tr>
<th>Import</th>
<td><code class="language-swift">import Swift</code></td>
</tr>

</table>


<h3>Initializers</h3>
<div class="declaration" id="init">
<a class="toggle-link" data-toggle="collapse" href="#comment-init">init()</a><div class="comment collapse" id="comment-init"><div class="p">
    <p>Construct a null pointer.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">init()</code>

    </div></div>
</div>
<div class="declaration" id="init_-copaquepointer">
<a class="toggle-link" data-toggle="collapse" href="#comment-init_-copaquepointer">init(<wbr>_: COpaquePointer)</a><div class="comment collapse" id="comment-init_-copaquepointer"><div class="p">
    <p>Convert from an opaque C pointer to a typed pointer</p>

<p>This is a fundamentally unsafe conversion.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">init(_ other: COpaquePointer)</code>

    </div></div>
</div>
<div class="declaration" id="init-u_-unsafemutablepointer-u">
<a class="toggle-link" data-toggle="collapse" href="#comment-init-u_-unsafemutablepointer-u">init&lt;U&gt;(<wbr>_: UnsafeMutablePointer&lt;U&gt;)</a><div class="comment collapse" id="comment-init-u_-unsafemutablepointer-u"><div class="p">
    <p>Convert from an UnsafeMutablePointer of a different type.</p>

<p>This is a fundamentally unsafe conversion.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">init&lt;U&gt;(_ from: UnsafeMutablePointer&lt;U&gt;)</code>

    </div></div>
</div>
<div class="declaration" id="init-u_-unsafepointer-u">
<a class="toggle-link" data-toggle="collapse" href="#comment-init-u_-unsafepointer-u">init&lt;U&gt;(<wbr>_: UnsafePointer&lt;U&gt;)</a><div class="comment collapse" id="comment-init-u_-unsafepointer-u"><div class="p">
    <p>Convert from a UnsafePointer of a different type.</p>

<p>This is a fundamentally unsafe conversion.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">init&lt;U&gt;(_ from: UnsafePointer&lt;U&gt;)</code>

    </div></div>
</div>
<div class="declaration" id="init-bitpattern_-uword">
<a class="toggle-link" data-toggle="collapse" href="#comment-init-bitpattern_-uword">init(<wbr>bitPattern: UWord)</a><div class="comment collapse" id="comment-init-bitpattern_-uword"><div class="p">
    <p>Construct an <code>UnsafePointer</code> from a given address in memory.</p>

<p>This is a fundamentally unsafe conversion.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">init(bitPattern: UWord)</code>

    </div></div>
</div>
<div class="declaration" id="init-bitpattern_-word">
<a class="toggle-link" data-toggle="collapse" href="#comment-init-bitpattern_-word">init(<wbr>bitPattern: Word)</a><div class="comment collapse" id="comment-init-bitpattern_-word"><div class="p">
    <p>Construct an <code>UnsafePointer</code> from a given address in memory.</p>

<p>This is a fundamentally unsafe conversion.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">init(bitPattern: Word)</code>

    </div></div>
</div>
<div class="declaration" id="init-nilliteral_">
<a class="toggle-link" data-toggle="collapse" href="#comment-init-nilliteral_">init(<wbr>nilLiteral:)</a><div class="comment collapse" id="comment-init-nilliteral_"><div class="p">
    <p>Create an instance initialized with <code>nil</code>.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">init(nilLiteral: ())</code>

    </div></div>
</div>


<h3>Instance Variables</h3>
<div class="declaration" id="var-debugdescription_-string">
<a class="toggle-link" data-toggle="collapse" href="#comment-var-debugdescription_-string">var debugDescription: String</a><div class="comment collapse" id="comment-var-debugdescription_-string"><div class="p">
    <p>A textual representation of <code>self</code>, suitable for debugging.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">var debugDescription: String { get }</code>

    </div></div>
</div>
<div class="declaration" id="var-hashvalue_-int">
<a class="toggle-link" data-toggle="collapse" href="#comment-var-hashvalue_-int">var hashValue: Int</a><div class="comment collapse" id="comment-var-hashvalue_-int"><div class="p">
    <p>The hash value.</p>

<p><strong>Axiom:</strong> <code>x == y</code> implies <code>x.hashValue == y.hashValue</code></p>

<p><strong>Note:</strong> the hash value is not guaranteed to be stable across
different invocations of the same program.  Do not persist the
hash value across program runs.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">var hashValue: Int { get }</code>

    </div></div>
</div>
<div class="declaration" id="var-memory_-t">
<a class="toggle-link" data-toggle="collapse" href="#comment-var-memory_-t">var memory: T</a><div class="comment collapse" id="comment-var-memory_-t"><div class="p">
    <p>Access the underlying raw memory, getting and
setting values.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">var memory: T { get }</code>

    </div></div>
</div>

<h3>Subscripts</h3>
<div class="declaration" id="subscript-subscript_-int">
<a class="toggle-link" data-toggle="collapse" href="#comment-subscript-subscript_-int">subscript(_: Int)</a>
<div class="comment collapse" id="comment-subscript-subscript_-int"><div class="p">
    

    <h4>Declaration</h4>    
    <code class="language-swift">subscript(i: Int) -&gt; T { get }</code>
    
    
</div></div>
</div>


<h3>Instance Methods</h3>
<div class="declaration" id="func-advancedby_">
<a class="toggle-link" data-toggle="collapse" href="#comment-func-advancedby_">func advancedBy(<wbr>_:)</a>
        
<div class="comment collapse" id="comment-func-advancedby_"><div class="p">
    <p>Return <code>self</code> offset by <code>n</code> steps.</p>

<p><strong>Returns:</strong> If <code>n &gt; 0</code>, the result of applying <code>successor</code> to
<code>self</code> <code>n</code> times.  If <code>n &lt; 0</code>, the result of applying
<code>predecessor</code> to <code>self</code> <code>-n</code> times. Otherwise, <code>self</code>.</p>

<p>Complexity: O(1)</p>

    <h4>Declaration</h4>    
    <code class="language-swift">func advancedBy(n: Int) -&gt; UnsafePointer&lt;T&gt;</code>
    
    
</div></div>
</div>
<div class="declaration" id="func-distanceto_">
<a class="toggle-link" data-toggle="collapse" href="#comment-func-distanceto_">func distanceTo(<wbr>_:)</a>
        
<div class="comment collapse" id="comment-func-distanceto_"><div class="p">
    <p>Return the minimum number of applications of <code>successor</code> or
<code>predecessor</code> required to reach <code>other</code> from <code>self</code>.</p>

<p>Complexity: O(1).</p>

    <h4>Declaration</h4>    
    <code class="language-swift">func distanceTo(x: UnsafePointer&lt;T&gt;) -&gt; Int</code>
    
    
</div></div>
</div>
<div class="declaration" id="func-encode">
<a class="toggle-link" data-toggle="collapse" href="#comment-func-encode">func encode()</a>
        
<div class="comment collapse" id="comment-func-encode"><div class="p">
    <p>Transform <code>self</code> into a series of machine words that can be
appropriately interpreted by C varargs</p>

    <h4>Declaration</h4>    
    <code class="language-swift">func encode() -&gt; [Word]</code>
    
    
</div></div>
</div>
<div class="declaration" id="func-getmirror">
<a class="toggle-link" data-toggle="collapse" href="#comment-func-getmirror">func getMirror()</a>
        
<div class="comment collapse" id="comment-func-getmirror"><div class="p">
    <p>Returns a mirror that reflects <code>self</code>.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">func getMirror() -&gt; MirrorType</code>
    
    
</div></div>
</div>
<div class="declaration" id="func-predecessor">
<a class="toggle-link" data-toggle="collapse" href="#comment-func-predecessor">func predecessor()</a>
        
<div class="comment collapse" id="comment-func-predecessor"><div class="p">
    <p>Returns the previous consecutive value before <code>self</code>.</p>

<p>Requires: the previous value is representable.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">func predecessor() -&gt; UnsafePointer&lt;T&gt;</code>
    
    
</div></div>
</div>
<div class="declaration" id="func-successor">
<a class="toggle-link" data-toggle="collapse" href="#comment-func-successor">func successor()</a>
        
<div class="comment collapse" id="comment-func-successor"><div class="p">
    <p>Returns the next consecutive value after <code>self</code>.</p>

<p>Requires: the next value is representable.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">func successor() -&gt; UnsafePointer&lt;T&gt;</code>
    
    
</div></div>
</div>


